/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.dispatch.web.api;

import cn.star.framework.dispatch.web.service.PlatformService;
import cn.star.framework.core.api.AppResult;
import com.xxl.adapter.entity.JobInfo;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 通知<br>
 *
 * @author 冰羽
 * @version 2.2.0
 */
@RestController
@RequestMapping("/dispatch/api")
public class ApiController {
  @Autowired private PlatformService platformService;

  private void wrapper(JobInfo info) {
    if (info != null && info.getGlueUpdatetime() == null) {
      info.setGlueUpdatetime(new Date());
    }
  }

  @PostMapping("/job/{type}")
  public AppResult<String> job(
      @PathVariable(value = "type") String type, @RequestBody JobInfo info) {

    this.wrapper(info);

    try {
      switch (type) {
        case "create":
          platformService.jobCreate(info);
          break;
        case "update":
          platformService.jobUpdate(info);
          break;
        case "upset":
          platformService.jobUpset(info);
          break;
        case "delete":
          platformService.jobDelete(info.getKey());
          break;
        case "start":
          platformService.jobStart(info.getKey());
          break;
        case "stop":
          platformService.jobStop(info.getKey());
          break;
        default:
          throw new RuntimeException(
              "不支持的类型 " + type + "，支持的类型有：create|update|upset|delete|start|stop");
      }
      return AppResult.ofSuccess();
    } catch (Exception e) {
      return AppResult.ofFail(e.getMessage());
    }
  }

  @PostMapping(value = "/broadcast")
  public AppResult<String> broadcast(@RequestBody JobInfo info) {
    try {
      platformService.broadcast(info.getExecutorHandler(), info.getExecutorParam());
      return AppResult.ofSuccess();
    } catch (Exception e) {
      return AppResult.ofFail(e.getMessage());
    }
  }
}
